<?php
/**
 * Yotaku
 * Copyright (c) 2016 http://www.yotaku.net All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.net>
 */

namespace Addons\Tags;
use Common\Controller\Addon;

class TagsAddon extends Addon{

    /**
     * @describe 后台表单扩展钩子
     * @author Creatint
     * @param $info
     */
    public function admin_goods_form($info){
        // 获取配置
        $config = $this->getConfig();
        
        // 获取商品标签数据
        $TagsModel = D("Addons://Tags/Tags");
        $tags = $TagsModel->get_tags_by_goods_id($info['id']);
        $this->assign("tags", $tags);
        

        $this->assign("config", $config);
        
        $this->display("admin_form");
    }

    public function admin_goods_post($post){
        // 获取表单数据
        $tags = $post['tags'];
        
        // 清除商品标签
        $TagsModel = D("Addons://Tags/Tags");
        $TagsModel->clear_by_goods_id($post['id']);

        // 保存标签数据
        $TagsModel->save_with_goods_id($tags, $post['id']);
    }
    
    
    public function goods_detail($info){
        // 获取插件配置
        $config = $this->getConfig();
//        dump($config);

        // 由商品ID获取标签分类
        $tags_group = array();
        $TagsModel = D("Addons://Tags/Tags");
        $tags_group_res = $TagsModel->get_tags_by_goods_id($info['id'],"field","field");
        foreach($tags_group_res as $k=>$v){
            $tags_group[$k]["field"] = $v["field"];
            foreach($config["tags"] as $v2){
                if($v2["field"] == $v["field"]){
                    $tags_group[$k]["title"] = $v2["title"];
                    $tags_group[$k]["type"] = $v2["type"];
                }
            }
        }
        $this->assign("tags_group",$tags_group);

        // 获取该商品全部标签
        $tags = $TagsModel->get_tags_by_goods_id($info['id']);
        $this->assign("tags",$tags);
//        dump($tags);


        $this->display("select");
    }
    

    public function order_goods_confirm($tags){
        $this->assign("tags", $tags);

        // 获取配置
        $config = $this->getConfig();
        $tags_config = array();
        foreach($config['tags'] as $v){
            $tags_config[$v['field']] = $v['title'];
        }
        $this->assign("tags_config",$tags_config);

        $this->display("show");
    }
    public function order_goods_detail($tags){
        $this->assign("tags", $tags);

        // 获取配置
        $config = $this->getConfig();
        $tags_config = array();
        foreach($config['tags'] as $v){
            $tags_config[$v['field']] = $v['title'];
        }
        $this->assign("tags_config",$tags_config);

        $this->display("show");
    }
    public function order_goods_show($tags){
        // 如果是字符串，尝试json解码
        if(is_string($tags))
            $tags = json_decode($tags, true);

        // 修改key为field，同时检验是否为数组
        $tags_tmp = array();
        if(is_array($tags)){
            foreach($tags as $tag){
                $tags_tmp[$tag["field"]] = $tag;
            }
        }else{
            return;
        }
        
        // 传值到模板
        $this->assign("tags", $tags);

        // 获取配置
        $config = $this->getConfig();
        $tags_config = array();
        foreach($config['tags'] as $v){
            $tags_config[$v['field']] = $v['title'];
        }
        
        // 传值到模板
        $this->assign("tags_config",$tags_config);

        $this->display("show");
    }

    public function uninstall(){

    }
    public function install(){

    }
}